package com.fruitstore.jdbc2;

import com.alibaba.druid.pool.DruidDataSource;
import org.junit.Test;

import java.sql.*;

public class JDBCDemoBatch {
    final String DRIVER = "com.mysql.cj.jdbc.Driver";
    final String URL = "jdbc:mysql://localhost:3306/test_fruit_java12?serverTimezone=Asia/Shanghai&characterEncoding=UTF-8";
    final String USER = "root";
    final String PWD = "5200201";
    protected Connection conn ;
    protected PreparedStatement psmt ;
    protected ResultSet rs ;

    @Test
    public void addUaer(){
        try {
//            Class.forName(DRIVER);
//            conn = DriverManager.getConnection(URL, USER, PWD);
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(DRIVER);
            dataSource.setUrl(URL);
            dataSource.setUsername(USER);
            dataSource.setPassword(PWD);
            conn=dataSource.getConnection();
            String sql="insert into t_user(name,pass) values(?,?)";
            psmt=conn.prepareStatement(sql);
            for (int i = 0; i < 10; i++) {
                psmt.setString(1,"de"+i);
                psmt.setString(2,"123"+i);
                psmt.addBatch();
            }
            int[] ints = psmt.executeBatch();
            for (int i = 0; i < ints.length; i++) {
                System.out.println(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
